package com.yanggu.scala.chapater12.object2

object MatchObjectDemo02 {

  def main(args: Array[String]): Unit = {
    val nameString = "Alice,Bob,Tom"
    //当case后面的对象提取器方法的参数有多个时, 默认会调用unapplySeq()方法。
    //将返回得到的值依次赋值给first、second、third
    nameString match {
      case Names(first, second, third) =>
        printf(s"$first, $second, $third")
      case _ => println("Nothing matched")
    }
  }

}

object Names {

  def unapplySeq(str: String): Option[Seq[String]] = {
    if (str.contains(",")) {
      Some(str.split(","))
    } else {
      None
    }
  }

}
